#! /user/in/env python
#Program to remove spaces from the Excel VBA sample code

print "Remove the spaces from file:"
newFile = "with line breaks.txt"
sourceFile = "file to reformat line breaks.txt"

txtFile = open(sourceFile)
pageBreak = "\n"


txt = txtFile.read()
txtFile.close()

#start out the maxWidth at the length of the string to be safe
maxSpaceWidth = txt.__len__()
print maxSpaceWidth, " Max"


#Loop through from the max # of touching spaces to the
#smallest number in consideration: 2
for spaceWidth in range(maxSpaceWidth,1,-1):
    #Replace all occurences of spaceWidth with the desired line break

    #current long space substring being searched for
    concatSpaces = spaceWidth * " "
    #print spaceWidth
    #print "Currently Searching for: ", spaceWidth * "-"
    
    #replace all occurences of the large spaces with page breaks
    txt = txt.replace(concatSpaces,pageBreak)
        
    
#save modified txt to a new file

print "\n", "saved to file", newFile

output = open(newFile,"w")
output.write(txt)

#close is necessary for windows to see the changes written to the file
output.close()

print txt
